package com.order.manager.controller.Evaluate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.order.manager.Model.EvaluateInfo;
import com.order.manager.Model.OrderInfo;
import com.order.manager.Model.ShopInfo;
import com.order.manager.Model.other.RestBean;
import com.order.manager.service.impl.EvaluateInfoServiceImpl;
import com.order.manager.service.impl.OrderInfoServiceImpl;
import com.order.manager.service.impl.ShopInfoServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/api/evaluate")
public class EvaluateController {

    @Resource
    EvaluateInfoServiceImpl evaluateInfoService;

    @Resource
    ShopInfoServiceImpl shopInfoService;

    @Resource
    OrderInfoServiceImpl orderInfoService;

    @RequestMapping("/create")
    public RestBean createEvaluate(@RequestBody EvaluateInfo evaluateInfo){
        if (evaluateInfoService.getOne(new QueryWrapper<EvaluateInfo>()
                .eq("order_id",evaluateInfo.getOrderId())
        ) != null) {
           return RestBean.failure(403,"该订单已评论，请勿重复评论");
        }
        String uuid = UUID.randomUUID().toString();
        evaluateInfo.setEvaluateId(uuid);
        evaluateInfo.setInputTime(new Date());
        evaluateInfo.setUpdateTime(null);
        OrderInfo orderInfo = orderInfoService.getById(evaluateInfo.getOrderId());
        orderInfo.setApplyState(2);
        orderInfo.setUpdateTime(new Date());
        orderInfoService.updateById(orderInfo);
        return evaluateInfoService.save(evaluateInfo)?RestBean.success(evaluateInfo):
                RestBean.failure(500,"评价失败");
    }

    @RequestMapping("/select/{orderId}")
    public RestBean select(@PathVariable String orderId){
        EvaluateInfo evaluateInfo = evaluateInfoService.getOne(new QueryWrapper<EvaluateInfo>()
                .eq("order_id",orderId)
        );
        return evaluateInfo == null ?RestBean.failure(403,"没有该订单信息")
                :RestBean.success(evaluateInfo);
    }

    @RequestMapping("/select/shopId/{shopId}")
    public RestBean getByShopId(@PathVariable String  shopId){
        return RestBean.success(evaluateInfoService.queryByShopId(shopId));
    }





}
